/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

/*
 * ImageFilter.java
 *
 * Copyright 2010 Universidad Francisco de Paula Santander (UFPS).
 * Todos los Derechos Reservados.
 * Creado el 12/07/2010, 09:38:45 PM
 */
package com.ufps.cuatro.util;

import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;

/* ImageFilter.java is used by FileChooserDemo2.java. */
/**
 * <i><b>ImageFilter:</b></i> @DefinicionClase
 * @author Carlos A. Torres C. - Dolly E. Guerrero. <https://code.google.com/p/tesis4dor/>
 * @version 1.0 12/07/2010
 * @see FileFilter
 */
public class ImageFilter extends FileFilter {

    /**
     * Establece cadena o extension para la imagen como jpeg
     */
    public final static String jpeg = "jpeg";
    /**
     * Establece cadena o extension para la imagen como jpg
     */
    public final static String jpg = "jpg";
    /**
     * Establece cadena o extension para la imagen como gif
     */
    public final static String gif = "gif";
    /**
     * Establece cadena o extension para la imagen como tiff
     */
    public final static String tiff = "tiff";
    /**
     * Establece cadena o extension para la imagen como tif
     */
    public final static String tif = "tif";
    /**
     * Establece cadena o extension para la imagen como png
     */
    public final static String png = "png";
    /**
     * Establece cadena o extension como 4dor
     */
    public final static String dor = "4dor";
    /**
     * Establece cadena o extension como jar
     */
    public final static String jar = "jar";
    /**
     * Establece cadena o extension como 4lib
     */
    public final static String lib = "4lib";
    
    /**
     * @DefinicionAtributo
     */
    public final static int MODO_DOR = 0;
    /**
     * @DefinicionAtributo
     */
    public final static int MODO_IMG = 1;
    /**
     * @DefinicionAtributo
     */
    public final static int MODO_JAR = 2;
    /**
     * @DefinicionAtributo
     */
    public final static int MODO_LIB = 3;
    
    private int modo;

    /**
     * Constructor por defecto de la clase
     */
    public ImageFilter() {
        modo = ImageFilter.MODO_DOR;
    }

    /**
     * @DefinicionMetodo
     * @param modo @DefinicionParametro
     */
    public ImageFilter(int modo) {
        this.modo = modo;
    }

    /*
     * Get the extension of a file.
     */
    /**
     * Devuelve la extension o ruta donde se encuentra guardado el archivo
     * @param f @DefinicionParametro
     * @return @DefinicionReturn
     */
    public static String getExtension(File f) {
       try{
            String ext = null;
            String s = f.getName();
            int i = s.lastIndexOf('.');

            if (i > 0 &&  i < s.length() - 1) {
                ext = s.substring(i+1).toLowerCase();
            }
            return ext;
        }catch(java.lang.NullPointerException ex){}
       return "";
    }

    /** Returns an ImageIcon, or null if the path was invalid.
     * @param path @DefinicionParametro
     * @return   @DefinicionReturn
     */
    protected static ImageIcon createImageIcon(String path) {
        try{
            java.net.URL imgURL = ImageFilter.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }catch(java.lang.NullPointerException ex){}
        return null;
    }

    //Accept all directories and all gif, jpg, tiff, or png files.
    public boolean accept(File f) {
       try{
        if (f.isDirectory()) {
            return true;
        }

        String extension = ImageFilter.getExtension(f);
        if (extension != null) {
            switch(modo){
                case ImageFilter.MODO_DOR:
                    if (extension.equals(ImageFilter.dor)) {
                            return true;
                    } else {
                        return false;
                    }
                case ImageFilter.MODO_IMG:
                    if (extension.equals(ImageFilter.tiff) ||
                        extension.equals(ImageFilter.tif) ||
                        extension.equals(ImageFilter.gif) ||
                        extension.equals(ImageFilter.jpeg) ||
                        extension.equals(ImageFilter.jpg) ||
                        extension.equals(ImageFilter.png)) {
                            return true;
                    } else {
                        return false;
                    }
                case ImageFilter.MODO_JAR:
                    if (extension.equals(ImageFilter.jar)) {
                            return true;
                    } else {
                        return false;
                    }
                case ImageFilter.MODO_LIB:
                    if (extension.equals(ImageFilter.lib)) {
                            return true;
                    } else {
                        return false;
                    }
            }
            
        }
        }catch(java.lang.NullPointerException ex){}

        return false;
    }

    //The description of this filter
    public String getDescription() {
       try{
            switch(modo){
                case ImageFilter.MODO_DOR:
                    return "Archivo 4Dor.";
                case ImageFilter.MODO_IMG:
                    return "Archivos de Imagen.";
                case ImageFilter.MODO_JAR:
                    return "Archivo Jar.";
                case ImageFilter.MODO_LIB:
                    return "Archivo de Biblioteca de Componentes.";
            }
            return "Todos los Archivos";
        }catch(java.lang.NullPointerException ex){}
       return "";
    }
}
